<?php

/*
 * huofu: it is used for signin to twitter or signout with different twitter id.
 */
/* Start session and load lib */
session_start();
require_once('JSON/JSON.php');
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');



/* Create TwitterOAuth object and get request token */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
if(isset($_REQUEST['force'])){
	/* Get request token with force twitter signin       */
	/* call back will go to OAUTH_CALLBACK_TW url setup  */
	$request_token = $connection->getRequestTokenForceSignin(OAUTH_CALLBACK_TW,true);
}
else {
	/* Get request token by use previous twitter signin*/
	/* call back will go to OAUTH_CALLBACK url setup  */
	$request_token = $connection->getRequestToken(OAUTH_CALLBACK);
}
/* Save request token to session */
$_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
 
/* If last connection fails don't display authorization link */
switch ($connection->http_code) {
  case 200:
    /* Build authorize URL */
	if(isset($_REQUEST['force'])){
		/* Get AuthorizeURL with force twitter signin       */  	
	  	$url = $connection->getAuthorizeURL($token,null);
	}
	else {
		/* Get AuthorizeURL by use previous twitter signin*/
	  	$url = $connection->getAuthorizeURL($token);
	}
	header('Location: ' . $url); 
    break;
  default:
    echo 'Could not connect to Twitter. Refresh the page or try again later.';
    break;
}
